package homework01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class dbTest {

    public static void main(String[] args)  {

        //1.获取连接
        Connection con = DBUtils.getConnection();

        //2.获取PrepareStatement 预处理对象
        //使用 ? 占位符的方式来设置参数
        String sql = "select * from employee where NAME = ? and gender = ?";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = con.prepareStatement(sql);
            //3.设置参数 使用setXXX(占位符的位置(整数),要设置的值)的方法设置占位符的参数
            ps.setString(1,"唐僧");
            ps.setString(2,"女");
            //4.执行查询
            resultSet = ps.executeQuery();
            show(resultSet);
            //再来一次
            ps.setString(1,"孙悟空");
            ps.setString(2,"男");
            resultSet = ps.executeQuery();
            show(resultSet);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //5.释放资源
            DBUtils.close(con,ps,resultSet);
        }


    }





    public static void show(ResultSet resultSet) throws SQLException {
        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String username = resultSet.getString("NAME");
            String gender = resultSet.getString("gender");
            String salary = resultSet.getString("salary");
            String bonus = resultSet.getString("bonus");
            String join_date = resultSet.getString("join_date");
            System.out.println(id+" 姓名为："+username+"，性别为 " + gender +"，工资为 " + salary + ", 奖金为" + bonus +",入职时间为"+join_date);
        }
    }


}

